<?php

namespace app\controllers;


use app\components\AppController;
use app\models\Categories;
use app\models\Post;
use yii\web\NotFoundHttpException;

class CategoryController extends AppController {

    public function actionIndex() {
        $slug = $this->get('slug');
        $category = Categories::find()->where(['slug' => $slug])->one();
        if (!$category) {
            throw new NotFoundHttpException("category id: {$slug} not found.");
        }

        $model = new Post();
        $model->defaultOrder = ['id' => SORT_ASC];
        $dataProvider = $model->search(['cid' => $category->id]);
        $models = $dataProvider->getModels();
        $pagination = $dataProvider->getPagination();

        $this->view->title = $category->title;
        $this->keywords = "{$category->title}";
        $this->description = $category->description;

        $view = '/' . $category->getViewName() . '/index';
        return $this->render(compact('models', 'pagination'), $view);
    }
}